set more off
capture log close

//  program:    AnanatEtAl_test_score_controls
//  task:		Runs supplemental regressions including controls for various time-varying state-level characteristics

//Change working directory to point to the file with the data
cd *** Fill in path name here ***

//Create the log file
log using AnanatEtAl_test_score_controls, text replace

//Load the test score data
use AnanatEtAl_test_score_data, clear

//set the panel variables
xtset stateno year

//#1
//GDP Per Capita
//#1a
//Standardize lagged GDP across states, by year
sort year
by year: egen meangdplag = mean(gdplag)
by year: egen sdgdplag = sd(gdplag)
gen z_gdplag = (gdplag - meangdplag)/sdgdplag
sort stateno year
//#1b
//Run regressions
** All Students **
ivregress 2sls z_all yrd* stated* z_gdplag (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using test_score_gdplag, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* z_gdplag (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using test_score_gdplag, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* z_gdplag (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using test_score_gdplag, drop(yrd* stated*) excel dec(3)

** State-years with below median unemployment **
//generate the median unemployment cutoff
sort year
preserve
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* z_gdplag (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using test_score_gdplag, drop(yrd* stated*) excel dec(3)

restore

** State-years with above median unemployment **
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* z_gdplag (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using test_score_gdplag, drop(yrd* stated*) excel dec(3)

clear all

//#2
//Lagged Unemployment
//Load the test score data
use AnanatEtAl_test_score_data, clear

//set the panel variables
xtset stateno year

//Run regressions
** All Students **
ivregress 2sls z_all yrd* stated* unemplag (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using test_score_unemplag, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* unemplag (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using test_score_unemplag, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* unemplag (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using test_score_unemplag, drop(yrd* stated*) excel dec(3)

** State-years with below median unemployment **
//generate the median unemployment cutoff
sort year
preserve
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* unemplag (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using test_score_unemplag, drop(yrd* stated*) excel dec(3)

restore

** State-years with above median unemployment **
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* unemplag (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using test_score_unemplag, drop(yrd* stated*) excel dec(3)

clear all

//#3
//Lagged Foreclosure Rate
//Load the test score data
use AnanatEtAl_test_score_data, clear

//set the panel variables
xtset stateno year

//Run regressions
** All Students **
ivregress 2sls z_all yrd* stated* fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using test_score_foreclosure, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using test_score_foreclosure, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using test_score_foreclosure, drop(yrd* stated*) excel dec(3)

** State-years with below median unemployment **
//generate the median unemployment cutoff
sort year
preserve
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using test_score_foreclosure, drop(yrd* stated*) excel dec(3)

restore

** State-years with above median unemployment **
//generate the median unemployment cutoff value
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using test_score_foreclosure, drop(yrd* stated*) excel dec(3)
clear all

//#4
//Demographic Subgroup Shares of State Populations
//Load the test score data
use AnanatEtAl_test_score_data, clear
sort stateno year

//#4a
//Merge with the ACS population data
merge 1:1 stateno year using AnanatEtAl_ACS_data
drop _merge

//set the panel variables
xtset stateno year

//#4b
//Run regressions
** All Students **
ivregress 2sls z_all yrd* stated* share_black share_white share_hispan share_minor share_elderly share_lesshs share_hsgrad share_somecoll share_poor (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using test_score_popshares, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* share_black share_white share_hispan share_minor share_elderly share_lesshs share_hsgrad share_somecoll share_poor (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using test_score_popshares, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* share_black share_white share_hispan share_minor share_elderly share_lesshs share_hsgrad share_somecoll share_poor (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using test_score_popshares, drop(yrd* stated*) excel dec(3)

** State-years with below median unemployment **
//generate the median unemployment cutoff
sort year
preserve
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* share_black share_white share_hispan share_minor share_elderly share_lesshs share_hsgrad share_somecoll share_poor (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using test_score_popshares, drop(yrd* stated*) excel dec(3)

restore

** State-years with above median unemployment **
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* share_black share_white share_hispan share_minor share_elderly share_lesshs share_hsgrad share_somecoll share_poor (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using test_score_popshares, drop(yrd* stated*) excel dec(3)
clear all

//#5
//Demographic Subgroup Shares of Student Population in the State
//Load the test score data
use AnanatEtAl_test_score_data, clear
sort stateno year

//set the panel variables
xtset stateno year

//run regressions
** All Students **
ivregress 2sls z_all yrd* stated* student_share* (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using test_score_studentshares, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* student_share* (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using test_score_studentshares, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* student_share* (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using test_score_studentshares, drop(yrd* stated*) excel dec(3)

** State-years with below median unemployment **
//generate the median unemployment cutoff
sort year
preserve
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* student_share* (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using test_score_studentshares, drop(yrd* stated*) excel dec(3)

restore

** State-years with above median unemployment **
//generate the median unemployment cutoff value
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* student_share* (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using test_score_studentshares, drop(yrd* stated*) excel dec(3)

clear all


//#6
//Controls: GDP per capita, Lagged Unemployment, Share Poor
//Load the test score data
use AnanatEtAl_test_score_data, clear
sort stateno year

//#6a
//Standardize lagged GDP across states, by year
sort year
by year: egen meangdplag = mean(gdplag)
by year: egen sdgdplag = sd(gdplag)
gen z_gdplag = (gdplag - meangdplag)/sdgdplag
sort stateno year

//#6b
//Merge with the ACS population data
merge 1:1 stateno year using AnanatEtAl_ACS_data
drop _merge

//set the panel variables
xtset stateno year

//#6c
//Run regressions
** All Students **
ivregress 2sls z_all yrd* stated* z_gdplag unemplag share_poor (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using test_score_controls_model6, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* z_gdplag unemplag share_poor (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using test_score_controls_model6, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* z_gdplag unemplag share_poor (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using test_score_controls_model6, drop(yrd* stated*) excel dec(3)

** State-years with below median unemployment **
//generate the median unemployment cutoff
sort year
preserve
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* z_gdplag unemplag share_poor (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using test_score_controls_model6, drop(yrd* stated*) excel dec(3)

restore

** State-years with above median unemployment **
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* z_gdplag unemplag share_poor (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using test_score_controls_model6, drop(yrd* stated*) excel dec(3)

clear all


//#7
//Controls: GDP per capita, Lagged Unemployment, Share Poor, Lagged Foreclosure Rate
//Load the test score data
use AnanatEtAl_test_score_data, clear
sort stateno year

//#7a
//Standardize lagged GDP across states, by year
sort year
by year: egen meangdplag = mean(gdplag)
by year: egen sdgdplag = sd(gdplag)
gen z_gdplag = (gdplag - meangdplag)/sdgdplag
sort stateno year

//#7b
//Merge with the ACS population data
merge 1:1 stateno year using AnanatEtAl_ACS_data
drop _merge

//set the panel variables
xtset stateno year

//#7c
//Run regressions
** All Students **
ivregress 2sls z_all yrd* stated* z_gdplag unemplag share_poor fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using test_score_controls_model7, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* z_gdplag unemplag share_poor fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using test_score_controls_model7, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* z_gdplag unemplag share_poor fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using test_score_controls_model7, drop(yrd* stated*) excel dec(3)

** State-years with below median unemployment **
//generate the median unemployment cutoff
sort year
preserve
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* z_gdplag unemplag share_poor fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using test_score_controls_model7, drop(yrd* stated*) excel dec(3)

restore

** State-years with above median unemployment **
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* z_gdplag unemplag share_poor fctotyearlg (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using test_score_controls_model7, drop(yrd* stated*) excel dec(3)

clear all

